package com.example.question.tree.tree2;

import com.example.question.tree.tree1.Tree;
import com.example.question.tree.tree1.TreeNode;

public class Code03_114 {
    public static void main(String[] args) {
        TreeNode node = Tree.createTree(new Integer[]{1, 2, 3, null, null, 4, null, null, 5, null, 6, null, null});
        Code03_114 code = new Code03_114();
        code.flatten(node);
        System.out.println(code);

    }

    public void flatten(TreeNode root) {
        transfer(root);
    }

    private void transfer(TreeNode node) {
        if (node == null) {
            return;
        }
        transfer(node.left);
        transfer(node.right);

        TreeNode right = node.right;
        node.right = node.left;
        TreeNode cur = node;
        // 找到尾巴
        while (cur.right != null) {
            cur = cur.right;
        }
        cur.right = right;

        node.left = null;
    }
}
